# -*- coding:utf-8 -*-
from flask import request, jsonify, abort
from flask_cors import CORS
from com.zzh.dao_v2.point_itemsDao import PointItemsDao
import time
from . import app

localTime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))


CORS(app, supports_credentials=True)



# ----------------------------------------point_items    积分项目表表API   --------------------------------------------


@app.route('/addPointItems', methods=['POST'])
def addPointItems():
    if not request.json:
        abort(400)

    pointitems_info = {
        "point_item_name": request.json['point_item_name'],
        "is_per_day": request.json['is_per_day'],
        "point_item_point": request.json['point_item_point'],
        "point_item_remark": request.json['point_item_remark'],
        "point_item_sort": request.json['point_item_sort']
    }
    try:
        rs = PointItemsDao().add_point_items(pointitems_info)
        if rs:
            return jsonify({  "message": {"code": 1, "info": "添加成功！"}})
        else:
            return jsonify({"message": {"code": 0, "info": "添加失败！"}})
    except Exception, e:
            return jsonify({"message": {"code": 0,  "info": str(e)}})


@app.route('/delPointItems', methods=['POST'])
def delPointItems():
    if not request.json:
        abort(400)
    ids = request.json['ids']
    try:
        PointItemsDao().del_point_items(ids)
        return jsonify({"message": {"code": 1, "info": " delete OK"}})
    except Exception, e:
            return jsonify({"message": {"code": 0,  "info": str(e)}})


@app.route('/queryPointItems', methods=['POST'])
def queryPointItems():

    if not request.json:
        abort(400)
    id = request.json.get("id")
    try:
        total_count, userCodeJSON= PointItemsDao().query_point_items(id)
        if total_count:
            return jsonify({"message": {"code": 1, "status": "queryProvince OK"},"info":userCodeJSON})
        else:
            return jsonify({"info": [], "message": {"code": 1, "status": "No data"}})
    except Exception, e:
            return jsonify({"message": {"code": 0,  "info": str(e)}})




@app.route('/queryPointItemsByCondition', methods=['POST'])
def queryPointItemsByCondition():
    if not request.json:
        abort(400)
    try:
        page = request.json['page']
        pageIndex = page["pageIndex"]
        pageSize = page["pageSize"]
    except:
        return jsonify({"message": {"code": 0, "status": "请传入 pageIndex和pageSize"}})
    try:
        conditions = request.json['condition']
    except:
        return jsonify({"message": {"code": 0, "status": "未传入指定condition"}})
    try:
        multiSort = request.json['multiSort']

    except:
        return jsonify({"message": "未传入排序规则multiSort"})
    try:
        total_count, UrlManageJSON = PointItemsDao().queryPointItemsByCondition(pageIndex=pageIndex, pageSize=pageSize,conditions=conditions, multiSort=multiSort)
        if total_count:
                return jsonify({"message": {"code": 1, "status": "queryPointItem OK"},"total":total_count,"info":UrlManageJSON})
        else:
            return jsonify({"info": [], "message": {"code": 1, "status": "No data"}})
    except Exception, e:
            return jsonify({"message": {"code": 0,  "info": str(e)}})

@app.route('/updatePointItem', methods=['POST'])
def updatePointItem():
    try:
        id = request.json['id']
    except:
        return jsonify({"message": {"code": 0, "status": "请传入 id"}})
    try:
        oldInfo = PointItemsDao().selectPointItems(id)
    except Exception, e:
            return jsonify({"message": {"code": 0,  "info": str(e)}})

    point_item_id = request.json.get('id')

    new_point_item = {
        'point_item_name': request.json.get('point_item_name',oldInfo[1]),
        'is_per_day': request.json.get('is_per_day',oldInfo[2]),
        'point_item_point': request.json.get('point_item_point',oldInfo[3]),
        'point_item_remark': request.json.get('point_item_remark',oldInfo[4]),
        'point_item_sort': request.json.get('point_item_sort',oldInfo[5])
    }
    if not new_point_item:
        return jsonify({"message": {"code": 0, "info": "信息填写未完整"}})
    try:
        updateresult = PointItemsDao().update_point_items(new_point_item, point_item_id)
        if updateresult:
            return jsonify({"info": updateresult,
                            "message": {"code": 1, "info": "update OK"}})
        else:
            return jsonify({"message": {"code": 0, "info": "update Failed"}})
    except Exception, e:
            return jsonify({"message": {"code": 0,  "info": str(e)}})


